package com.utils.basehelper.date;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class DateUtil1 {

	public static final String YMD = "yyyyMMdd";
	public static final String YMD_B = "yyyy-MM-dd";
	public static final String YMD_S = "yyyy/MM/dd";

	public static final String HMS = "hh:mm:ss";
	public static final String HMS_N = "hhmmss";

	public static final String FHMS = "HH:mm:ss";
	public static final String FHMS_N = "HHmmss";

	public static final String YMDHMS = YMD + " " + HMS;
	public static final String YMD_B_HMS = YMD_B + " " + HMS;
	public static final String YMD_S_HMS = YMD_S + " " + HMS;

	public static final String YMDFHMS = YMD + " " + FHMS;
	public static final String YMD_B_FHMS = YMD_B + " " + FHMS;
	public static final String YMD_S_FHMS = YMD_S + " " + FHMS;

	private static final String ISYMD = "[0-9]{4}[0-9]{2}[0-9]{2}";
	private static final String ISYMDB = "[0-9]{4}-[0-9]{2}-[0-9]{2}";
	private static final String ISYMDBS = "[0-9]{4}-[0-9]{2}";
	private static final String ISYMDS = "[0-9]{4}/[0-9]{2}/[0-9]{2}";

	/**
	 * 判断日期格式和范围 yyyy-MM-dd  yyyy/MM/dd  yyyyMMdd
	 * @param strDate
	 * @return
	 */
	public static boolean isDate(String strDate) {
		String rexp = "^((\\d{2}(([02468][048])|([13579][26]))[\\-\\/\\s]?"
				+ "((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])"
				+ "|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2]"
				+ "[0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])))))"
				+ "|(\\d{2}(([02468][1235679])|([13579][01345789]))[\\-\\/\\s]"
				+ "?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])"
				+ "|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2]"
				+ "[0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))";
		Pattern pat = Pattern.compile(rexp);
		Matcher mat = pat.matcher(strDate);
		boolean dateType = mat.matches();
		
		return dateType;
	}
	
	/**
	 * 简单判断字符串是否是yyyyMMdd格式
	 * @param strDate
	 * @return
	 */
	public static boolean isYMD(String strDate) {
		Pattern p = Pattern.compile(ISYMD);
		Matcher m = p.matcher(strDate);
		return m.matches();
	}
	
	/**
	 * 简单判断字符串是否是yyyy-MM-dd格式
	 * @param strDate
	 * @return
	 */
	public static boolean isYMDB(String strDate) {
		Pattern p = Pattern.compile(ISYMDB);
		Matcher m = p.matcher(strDate);
		return m.matches();
	}
	
	/**
	 * 简单判断字符串是否是yyyy-MM-dd格式
	 * @param strDate
	 * @return
	 */
	public static boolean isYMDBS(String strDate) {
		Pattern p = Pattern.compile(ISYMDBS);
		Matcher m = p.matcher(strDate);
		return m.matches();
	}
	
	/**
	 * 简单判断字符串是否是yyyy/MM/dd格式
	 * @param strDate
	 * @return
	 */
	public static boolean isYMDS(String strDate) {
		Pattern p = Pattern.compile(ISYMDS);
		Matcher m = p.matcher(strDate);
		return m.matches();
	}

	/**
	 * 根据字符串获取SimpleDateFormat对象，无时分秒
	 * @param strDate
	 * @return
	 * @throws Exception
	 */
	private static DateFormat getDateFormat(String strDate) throws Exception {
		DateFormat sdf = null;
		if (isYMD(strDate)) {
			sdf = new SimpleDateFormat(YMD);
		} else if (isYMDB(strDate)) {
			sdf = new SimpleDateFormat(YMD_B);
		} else if (isYMDS(strDate)) {
			sdf = new SimpleDateFormat(YMD_S);
		} else {
			throw new Exception("Date format cannot match");
		}
		return sdf;
	}
	
	/**
	 * 将Date类型转换成指定格式的字符串类型
	 * @param date
	 * @param pattern
	 * @return
	 */
	public static String formatString(Date date, String pattern) {
		if (date == null)
			return "";
		if (pattern == null)
			pattern = YMD_B;
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		sdf.setLenient(false);
		return sdf.format(date);
	}

	/**
	 * 将字符串类型转换成对应格式的Date类型 ,无时分秒
	 * @param strDate
	 * @param pattern
	 * @return
	 */
	public static Date formatDate(String strDate) {
		DateFormat sdf = null;
		Date date = new Date();
		try {
			sdf = getDateFormat(strDate);
			sdf.setLenient(false);
			date = sdf.parse(strDate);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return date;
	}

	/**
	 * 将字符串类型转换成指定格式的Date类型
	 * @param strDate
	 * @param pattern
	 * @return
	 */
	public static Date formatDate(String strDate, String pattern) {
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		Date date = null;
		try {
			sdf.setLenient(false);
			date = sdf.parse(strDate);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return date;
	}

	/**
	 * 根据日期获得偏移天数后的日期
	 * @param time
	 * @param offset 偏移天数
	 * @param pattern 格式
	 * @return 返回字符串类型
	 * @throws Exception 
	 */
	public static String offsetStr(Object time, int offset, String pattern) {
		DateFormat sdf = null;
		try {
			if (time instanceof String) {
				if(pattern!=null&&!"".equals(pattern.trim())) {
					sdf = new SimpleDateFormat(pattern);
				}else {
					sdf = getDateFormat(time.toString());
				}
				sdf.setLenient(false);
				Date date = new Date();
				long dif = sdf.parse(time.toString()).getTime() + 86400 * 1000 * offset;
				date.setTime(dif);
				
				return sdf.format(date);
			} else if (time instanceof Date) {
				sdf = getDateFormat(time.toString());
				long dif = ((Date) time).getTime() + 86400 * 1000 * offset;
				((Date) time).setTime(dif);
				
				return sdf.format((Date) time);
			} else {
				throw new Exception("Object format is incorrect");
			}
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 根据日期获得偏移天数后的日期
	 * @param time  日期
	 * @param offset 偏移天数
	 * @param pattern 格式
	 * @return 返回Date类型
	 */
	public static Date offsetDate(Object time, int offset, String pattern) {

		Date date = null;
		try {
			if (time instanceof String) {
				DateFormat sdf = null;
				if(pattern!=null&&!"".equals(pattern.trim())) {
					sdf = new SimpleDateFormat(pattern);
				}else {
					sdf = getDateFormat(time.toString());
				}
				sdf.setLenient(false);
				date = new Date();
				long dif = sdf.parse(time.toString()).getTime() + 86400 * 1000 * offset;
				date.setTime(dif);
				
			} else if (time instanceof Date) {
				date = (Date) time;
				long dif = date.getTime() + 86400 * 1000 * offset;
				date.setTime(dif);
			} else {
				throw new Exception("Object format is incorrect");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return date;
	}

	/**
	 * 根据Date类型的日期获得偏移天数后的日期
	 * @param beginDate
	 * @param offset
	 * @return
	 */
	public static Date add(Date beginDate, int offset){
		Calendar c = Calendar.getInstance();
		c.setTime(beginDate);
		c.set(Calendar.DATE, c.get(Calendar.DATE) + offset);
		return c.getTime();
	}

	/**
	 * 获得String类型的昨天日期,指定格式
	 * @param pattern
	 * @return
	 */
	public static String getYesterday(String pattern) {
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		Calendar c = Calendar.getInstance();
		c.add(Calendar.DAY_OF_MONTH, -1);
		return sdf.format(c.getTime());
	}

	/**
	 * 获得Date类型的昨天日期
	 * @return
	 */
	public static Date getYesterday() {
		Calendar c = Calendar.getInstance();
		c.add(Calendar.DAY_OF_MONTH, -1);
		return c.getTime();
	}

	/**
	 * 获得String类型的明天日期,指定格式
	 * @param pattern 输出格式
	 * @return
	 */
	public static String getTomorrow(String pattern) {
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		Calendar c = Calendar.getInstance();
		c.add(Calendar.DAY_OF_MONTH, 1);
		return sdf.format(c.getTime());
	}

	/**
	 * 获得Date类型的明天日期
	 * @return
	 */
	public static Date getTomorrow() {
		Calendar c = Calendar.getInstance();
		c.add(Calendar.DAY_OF_MONTH, 1);
		return c.getTime();
	}
	
	/**
	 * 取两个日期，相隔的天数
	 * @param stime
	 * @param etime
	 * @throws Exception 
	 */
	public static int differDay(Object stime,Object etime, String pattern) throws Exception {
		Date sdate = null;
		Date edate = null;
		DateFormat sdf = null;
		if(stime instanceof String) {
			if(pattern!=null&&!"".equals(pattern.trim())) {
				sdf = new SimpleDateFormat(pattern); 
			}else {
				sdf = getDateFormat(stime.toString()); 
			}
			sdate = sdf.parse(stime.toString());
		}else if(stime instanceof Date) {
			sdate = (Date)stime;
		}else {
			throw new Exception("Object format is incorrect");
		}
		
		if(etime instanceof String) {
			if(pattern!=null&&!"".equals(pattern.trim())) {
				sdf = new SimpleDateFormat(pattern); 
			}else {
				sdf = getDateFormat(etime.toString()); 
			} 
			edate = sdf.parse(etime.toString());	
		}else if(etime instanceof Date) {
			edate = (Date)etime;
		}else {
			throw new Exception("Object format is incorrect");
		}
		
		long st = sdate.getTime();
		long et = edate.getTime();
		long between_days = (et - st) / (1000 * 3600 * 24);
		
		return Integer.parseInt(String.valueOf(between_days));
	}
	/**
	 * 取的两个date类型日期，相隔的天数
	 * @param stime
	 * @param etime
	 */
	public static int differDay(Date sdate,Date edate) {
		
		/*long st = sdate.getTime();
		long et = edate.getTime();
		long between_days = (st - et) / (1000 * 3600 * 24);
		return Integer.parseInt(String.valueOf(between_days));*/
		Calendar cal = Calendar.getInstance();
		cal.setTime(sdate);
		long st = cal.getTimeInMillis();
		cal.setTime(edate);
		long et = cal.getTimeInMillis();
		long between_days = (et - st) / (1000 * 3600 * 24);
		return Integer.parseInt(String.valueOf(between_days));
		 	
	}
	
	/**
	 * 讲excel读取的日期数字转化为日期格式
	 * @param str 字符串形式的数字
	 * @return
	 */
	public static String excelDate(String str){
		 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		 Calendar ca = Calendar.getInstance();
         Date currdate;
         String enddate="";
		try {
			currdate = format.parse("1900-01-01");
            ca.setTime(currdate);
            ca.add(Calendar.DATE,Integer.valueOf(str)-2);
            currdate = ca.getTime();
            enddate = format.format(currdate);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		if("".equals(enddate)){
			enddate=format.format(new Date());
		}
		return enddate;
	}
	
	/**
	 * 将秒数转化为日期
	 * @param mills
	 * @param pattern
	 * @return
	 */
	public static String getDateByMill(long mills,String pattern) {
		SimpleDateFormat format = new SimpleDateFormat(pattern);
		Date date = new Date();
		date.setTime(mills*1000);
/*		Calendar c = Calendar.getInstance();
		c.setTimeInMillis(mills); 
		Date date = c.getTime();
		System.out.println(format.format(date));*/
		return format.format(date);
	}
	
	/**
	 * 将秒数转化为日期
	 * @param mills
	 * @param pattern
	 * @return
	 */
	public static Date getDateByMill(long mills) {
		Date date = new Date();
		date.setTime(mills*1000);
/*		Calendar c = Calendar.getInstance();
		c.setTimeInMillis(mills); 
		Date date = c.getTime();
		System.out.println(format.format(date));*/
		return date;
	}
	
	/**
	 * 将日期转化为毫秒数
	 * @param date
	 * @param pattern
	 * @return
	 */
	public static Long getMills(Object date,String pattern) {
		DateFormat sdf = null;
        long times = 0;
		try {
			if(date instanceof String) {
				if(pattern!=null&&!"".equals(pattern.trim())) {
					sdf = new SimpleDateFormat(pattern);
				}else {
					sdf = getDateFormat(date.toString());
				}
				
				times = sdf.parse(date.toString()).getTime();
			}else if(date instanceof Date) {
				sdf = new SimpleDateFormat();
				times = ((Date) date).getTime();
			}else {
				throw new Exception("Object format is incorrect");
			}
			
		} catch (ParseException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}  
        
		return times;
	}
	
	
	public static void main(String[] args) {
/*		String str = null;
		Date dt = new Date();
		
		str = DateUtil.formatString(new Date(), DateUtil.YMD_S);
		str = DateUtil.formatString(getTomorrow(), YMD_B);

		int	s = differDay(new Date(),offsetDate("2018-03-06", 2, null));
		System.out.println(s);
		System.out.println(str);
		System.out.println(dt);
		1521016346000*/
//     	System.out.println(getDateByMill(1000000000l, YMD_B_HMS));
//     	System.out.println(DateUtil.getMills(new Date(), YMDFHMS)/1000);
//     	System.out.println(DateUtil.getMills(new Date(), null)/1000);
		System.out.println(DateUtil1.isYMDBS("2018-09"));

	}
	
	public static String converTimestamp(String timestamp) {
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm");//这个是转成后的时间的格式
		String result = sdf.format(new Date(Long.parseLong(timestamp)));
		return result;
	}
	
	
}
